﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
   
    #region variable
    Dataconnect conn = new Dataconnect();
    SqlDataAdapter apter;
    DataSet set;
    static DataTable tblgiohang = new DataTable();
     
    int Quantity = 1;
    int id;
    SqlCommand cmd = null;
    SqlDataReader rd;
   
    #endregion
    #region method
    public void FillDdl() {

        cmd = new SqlCommand();
        cmd.Connection = conn.Connect();
        cmd.CommandText = "searchCategory";
        cmd.CommandType = CommandType.StoredProcedure;
       
        apter = new SqlDataAdapter(cmd);
        set = new DataSet();
        apter.Fill(set);

        DropDownList1.DataSource = set;
        DropDownList1.DataTextField = "Category_Name";
        DropDownList1.DataValueField = "Category_Name";
        DropDownList1.DataBind();
    }
    public void Load_data()
    {
        cmd = new SqlCommand();
        cmd.Connection = conn.Connect();
        cmd.CommandText = "tblProDetail";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", int.Parse(Session["ProID"].ToString()));
        apter = new SqlDataAdapter(cmd);
        set = new DataSet();
        apter.Fill(set);
        DataList_product.DataSource = set;
        DataList_product.DataBind();
        CollectionPager1.MaxPages = 10000;

        CollectionPager1.PageSize = 6; // số items hiển thị trên một trang

        CollectionPager1.DataSource = set.Tables[0].DefaultView;

        CollectionPager1.BindToControl = DataList_product;

        DataList_product.DataSource = CollectionPager1.DataSourcePaged;

        DataList_product.DataBind();
    }
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            if (Session["Giohang"] == null)
            {
                tblgiohang.Rows.Clear();
                tblgiohang.Columns.Clear();
                tblgiohang.Columns.Add("ProductID", typeof(int));
                tblgiohang.Columns.Add("ProductName", typeof(string));
                tblgiohang.Columns.Add("Price", typeof(float));
                tblgiohang.Columns.Add("Quantity", typeof(int));
                tblgiohang.Columns.Add("Total", typeof(double), "Quantity * Price");
            }
            else 
            {
                tblgiohang = (DataTable)Session["Giohang"];
            }
           
                if (Session["CustomerName"] != null)
                {
                    lbtn_login.Visible = false;
                    lbtn_register.Visible = false;
                    name.Text = Session["CustomerName"].ToString();
                }
                else
                {
                    lbtn_login.Visible = true;
                    lbtn_register.Visible = true;
                    name.Text = null;
                    lbtn_logout.Visible = false;
                }
                FillDdl();
            Load_data();
        }
        
    }
    protected void DataList_product_ItemCommand(object source, DataListCommandEventArgs e)
    {


        if (e.CommandName == "buy")
        {
            if (Session["CustomerName"] == null)
            {
                Response.Redirect("LogIn Customer.aspx");
            }
            else
            {
                int ProId = int.Parse(DataList_product.DataKeys[e.Item.ItemIndex].ToString());
                string ProName = ((Label)e.Item.FindControl("lbl_nameProduct")).Text;
                float Price = float.Parse(((Label)e.Item.FindControl("lbl_price")).Text);
                tblgiohang.Rows.Add(ProId, ProName, Price, Quantity);
                Response.Write("<script>alert('Add to cart successfully !!.')</script>");
                Session["Giohang"] = tblgiohang;
            }
        }
        if (e.CommandName == "Info")
        {
            int ProId = int.Parse(DataList_product.DataKeys[e.Item.ItemIndex].ToString());
            Session["ProID"] = ProId;
            Response.Redirect("Info_Products.aspx");
        }
    }
    protected void btn_search_Click(object sender, EventArgs e)
    {
        Search sea = new Search();
        id = sea.SearchId(DropDownList1.SelectedValue);
        cmd = new SqlCommand();
        cmd.Connection = conn.Connect();
        cmd.CommandText = "procSearchPrd";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@name", txt_name.Text);
        cmd.Parameters.AddWithValue("@IdCategory", id);

        apter = new SqlDataAdapter(cmd);
        set = new DataSet();
        apter.Fill(set);
        DataList_product.DataSource = set;
        DataList_product.DataBind();
        CollectionPager1.MaxPages = 10000;

        CollectionPager1.PageSize = 6; // số items hiển thị trên một trang

        CollectionPager1.DataSource = set.Tables[0].DefaultView;

        CollectionPager1.BindToControl = DataList_product;

        DataList_product.DataSource = CollectionPager1.DataSourcePaged;

        DataList_product.DataBind();

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void lbtn_logout_Click(object sender, EventArgs e)
    {
        Session["CustomerName"] = null;
        lbtn_logout.Visible = false;
        lbtn_register.Visible = true;
        name.Visible = false;
        lbtn_login.Visible = true;
        Response.Redirect("LogIn Customer.aspx");
    }
    protected void lbtn_register_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register.aspx");
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }
}